#!/usr/bin/env python

import os

VERSION="1.1.3"

def configure(conf):
    if conf.CHECK_BUNDLED_SYSTEM('nss_wrapper', minversion=VERSION, set_target=False):
        conf.DEFINE('USING_SYSTEM_NSS_WRAPPER', 1)
        libnss_wrapper_so_path = 'libnss_wrapper.so'
    else:
        conf.CHECK_HEADERS('nss.h')

        # check HAVE_GCC_THREAD_LOCAL_STORAGE
        conf.CHECK_CODE('''
            __thread int tls;

            int main(void) {
                return 0;
            }
            ''',
            'HAVE_GCC_THREAD_LOCAL_STORAGE',
            addmain=False,
            msg='Checking for thread local storage')

        # check HAVE_DESTRUCTOR_ATTRIBUTE
        conf.CHECK_CODE('''
            void test_destructor_attribute(void) __attribute__ ((destructor));

            void test_destructor_attribute(void)
            {
                return;
            }

            int main(void) {
                return 0;
            }
            ''',
            'HAVE_DESTRUCTOR_ATTRIBUTE',
            addmain=False,
            msg='Checking for library destructor support')

        conf.CHECK_FUNCS('gethostbyaddr_r gethostbyname_r')
        # Solaris
        conf.CHECK_FUNCS('__posix_getpwnam_r __posix_getpwuid_r')
        conf.CHECK_FUNCS('__posix_getgrgid_r __posix_getgrnam_r')

        conf.CHECK_FUNCS_IN('nsl',
                            'gethostname',
                            checklibc=True,
                            headers='unistd.h')

        # Prototype checks
        conf.CHECK_C_PROTOTYPE('getpwent_r',
                               'struct passwd *getpwent_r(struct passwd *src, char *buf, int buflen)',
                               define='HAVE_SOLARIS_GETPWENT_R', headers='unistd.h pwd.h')
        conf.CHECK_C_PROTOTYPE('getpwnam_r',
                               'int getpwnam_r(const char *name, struct passwd *pwd, char *buf, int buflen, struct passwd **ppwd)',
                               define='HAVE_SOLARIS_GETPWNAM_R', headers='unistd.h pwd.h')
        conf.CHECK_C_PROTOTYPE('getpwuid_r',
                               'int getpwuid_r(uid_t uid, struct passwd *pwd, char *buf, int buflen, struct passwd **ppwd)',
                               define='HAVE_SOLARIS_GETPWUID_R', headers='unistd.h pwd.h')
        conf.CHECK_C_PROTOTYPE('getgrent_r',
                               'struct group *getgrent_r(struct group *src, char *buf, int buflen)',
                               define='HAVE_SOLARIS_GETGRENT_R', headers='unistd.h grp.h')
        conf.CHECK_C_PROTOTYPE('getgrnam_r',
                               'int getgrnam_r(const char *name, struct group *grp, char *buf, int buflen, struct group **pgrp)',
                               define='HAVE_SOLARIS_GETGRNAM_R', headers='unistd.h grp.h')
        conf.CHECK_C_PROTOTYPE('getgrgid_r',
                               'int getgrgid_r(gid_t gid, struct group *grp, char *buf, int buflen, struct group **pgrp)',
                               define='HAVE_SOLARIS_GETGRGID_R', headers='unistd.h grp.h')
        conf.CHECK_C_PROTOTYPE('sethostent',
                               'int sethostent(int stayopen)',
                               define='HAVE_SOLARIS_SETHOSTENT', headers='unistd.h netdb.h')
        conf.CHECK_C_PROTOTYPE('endhostent',
                               'int endhostent(void)',
                               define='HAVE_SOLARIS_ENDHOSTENT', headers='unistd.h netdb.h')
        conf.CHECK_C_PROTOTYPE('gethostname',
                               'int gethostname(char *name, int len)',
                               define='HAVE_SOLARIS_GETHOSTNAME', headers='unistd.h netdb.h')
        conf.CHECK_C_PROTOTYPE('setgrent',
                               'int setgrent(void)',
                               define='HAVE_BSD_SETGRENT', headers='unistd.h grp.h')
        conf.CHECK_C_PROTOTYPE('getnameinfo',
                               'int getnameinfo (const struct sockaddr *sa, socklen_t salen, char *host, socklen_t __hostlen, char *serv, socklen_t servlen, int flags)',
                               define='HAVE_LINUX_GETNAMEINFO', headers='unistd.h netdb.h')
        conf.CHECK_C_PROTOTYPE('getnameinfo',
                               'int getnameinfo (const struct sockaddr *sa, socklen_t salen, char *host, socklen_t __hostlen, char *serv, socklen_t servlen, unsigned int flags)',
                               define='HAVE_LINUX_GETNAMEINFO_UNSIGNED', headers='unistd.h netdb.h')

        # Create full path to nss_wrapper
        srcdir = os.path.realpath(conf.srcdir)
        libnss_wrapper_so_path = srcdir + '/bin/default/lib/nss_wrapper/libnss-wrapper.so'

    conf.DEFINE('LIBNSS_WRAPPER_SO_PATH', libnss_wrapper_so_path)
    conf.DEFINE('NSS_WRAPPER', 1)

def build(bld):
    if bld.CONFIG_SET("HAVE_NSS_H") and not bld.CONFIG_SET("USING_SYSTEM_NSS_WRAPPER"):
        # We need to do it this way or the library wont work.
        # Using private_library=True will add symbol version which
        # breaks preloading!
        bld.SAMBA_LIBRARY('nss_wrapper',
                          source='nss_wrapper.c',
                          deps='dl',
                          install=False,
                          realname='libnss-wrapper.so')
